feat: Add connection-level query timeout support#5
Open
Conversation
mateusaubin
requested changes
May 11, 2026
Collaborator
mateusaubin
left a comment
There was a problem hiding this comment.
issue: logic is scattered in multiple files, making further maintenance (rebase from upstream) hard.
please group all logic into a single place such that we can keep it isolated from upstream, my recommendation:
/** Utility for building query timeout call options. */
public final class QueryTimeoutCallOption implements CallOption {
private QueryTimeoutCallOption() {}
/**
* Merges base call options with extra options.
*
* @param baseOptions the base options
* @param extraOptions additional options to merge
* @return combined array of options
*/
public static CallOption[] mergeOptions(CallOption[] baseOptions, CallOption... extraOptions) {
if (extraOptions.length == 0) {
return baseOptions;
}
final CallOption[] combined = new CallOption[baseOptions.length + extraOptions.length];
System.arraycopy(baseOptions, 0, combined, 0, baseOptions.length);
System.arraycopy(extraOptions, 0, combined, baseOptions.length, extraOptions.length);
return combined;
}
/**
* Builds a CallOption array from a statement's timeout configuration.
*
* @param statement the statement to extract timeouts from
* @return CallOption array with timeout header, or empty array if no timeout configured
* @throws SQLException on error retrieving timeout values
*/
public static CallOption[] fromStatement(ArrowFlightInfoStatement statement)
throws SQLException {
final Duration statementTimeout = Duration.ofSeconds(statement.getQueryTimeout());
final Duration connectionTimeout =
Duration.ofSeconds(statement.getConnection().getConnectionQueryTimeoutSeconds());
return build(statementTimeout, connectionTimeout);
}
/**
* Builds a CallOption array from a statement ID and connection.
*
* @param statementId the statement ID to look up in the connection's statement map
* @param connection the Avatica connection to extract timeouts from
* @return CallOption array with timeout header, or empty array if no timeout configured
*/
public static CallOption[] fromStatementHandle(int statementId, AvaticaConnection connection) {
Duration statementTimeout = Duration.ZERO;
final AvaticaStatement stmt = connection.statementMap.get(statementId);
if (stmt != null) {
try {
statementTimeout = Duration.ofSeconds(stmt.getQueryTimeout());
} catch (SQLException ignored) {
// fall through to connection-level timeout
}
}
Duration connectionTimeout = Duration.ZERO;
if (connection instanceof ArrowFlightConnection) {
connectionTimeout =
Duration.ofSeconds(
((ArrowFlightConnection) connection).getConnectionQueryTimeoutSeconds());
}
return build(statementTimeout, connectionTimeout);
}
/**
* Builds a CallOption array containing query timeout header if a timeout is configured.
*
* @param statementTimeout timeout at statement level (ZERO means use connection-level)
* @param connectionTimeout timeout at connection level
* @return CallOption array with timeout header, or empty array if no timeout configured
*/
private static CallOption[] build(Duration statementTimeout, Duration connectionTimeout) {
final Duration effectiveTimeout =
!statementTimeout.isZero() ? statementTimeout : connectionTimeout;
if (effectiveTimeout.isZero()) {
return new CallOption[0]; // short-circuit
}
final FlightCallHeaders headers = new FlightCallHeaders();
headers.insert("x-query-timeout-ms", String.valueOf(effectiveTimeout.toMillis()));
return new CallOption[] {new HeaderCallOption(headers)};
}
}
Author
|
All comments handled. Kindly re-check. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Query Timeout Support via
x-query-timeout-msgRPC HeaderThis change adds support for propagating query timeouts to the IOMETE Flight SQL server using a custom
x-query-timeout-msgRPC header. When a timeout is configured, the header is attached to every query execution request, allowing the server to enforce it server-side.Statement-level timeout
Use the standard JDBC API on any
StatementorPreparedStatement:The header
x-query-timeout-ms: 30000will be sent with the request. A value of0means no timeout (default).Connection-level (global) timeout
Set a default timeout for all queries on a connection via the JDBC URL or
Properties:All statements on this connection will include
x-query-timeout-ms: 60000unless overridden at the statement level.Precedence
Statement-level timeout takes priority over the connection-level default. If
setQueryTimeout(0)is called on a statement, it falls back to the connection-level value. If neither is set, no header is sent.stmt.setQueryTimeout(30)x-query-timeout-ms: 30000?queryTimeout=60(no statement timeout)x-query-timeout-ms: 60000stmt.setQueryTimeout(30)+?queryTimeout=60x-query-timeout-ms: 30000